package com.unicom.projt.http;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.unicom.projt.Const;

import java.io.Serializable;


/**
 * 在独立于GUI的的线程里发起HTTP请求，将HTTP响应作为字符串并转换为类型T。
 *
 * @param <T>
 */
public abstract class AbstractHttpController<T extends Serializable> implements
        HttpEventListener<T> {

    private class HttpThread implements Runnable {
        private String paramValue;
        private UriConstants uri;

        public HttpThread(UriConstants uri, String paramValue) {
            this.uri = uri;
            this.paramValue = paramValue;
        }

        @Override
        public void run() {
//            Map<String, String> params = new HashMap<String, String>();
            // 参数名固定为“request”

            Log.d(Const.TAG,
                    "AbstractHttpController:request string:" + paramValue);
//                params.put("request", paramValue);
            try {
                String resp = mHttpConnection.post(uri, paramValue);
//				Serializer serializer = new Persister();
                try {
//					T t = serializer.read(clazz, resp);
                    Message successMsg = new Message();
                    successMsg.what = MSG_SUCCESS;

                    Bundle b = new Bundle();
                    b.putSerializable("response", resp);
                    successMsg.setData(b);

                    mGuiHandler.sendMessage(successMsg);

                } catch (Exception e) {
                    Log.e(Const.TAG,
                            "AbstractHttpController:response converting error");
                    e.printStackTrace();

                    Message dataErrorMsg = new Message();
                    dataErrorMsg.what = MSG_RESPONSE_DATA_ERROR;
                    mGuiHandler.sendMessage(dataErrorMsg);
                }

            } catch (NetworkingException e) {
                Log.e(Const.TAG,
                        "AbstractHttpController:network error");
                e.printStackTrace();

                Message abortMsg = new Message();
                abortMsg.what = MSG_NETWORK_ABORT;
                mGuiHandler.sendMessage(abortMsg);

            } catch (NetworkingAbortException e) {
                Log.e(Const.TAG,
                        "AbstractHttpController:network abort");

                Message networkErrorMsg = new Message();
                networkErrorMsg.what = MSG_NETWORK_ERROR;
                mGuiHandler.sendMessage(networkErrorMsg);
            }
        }
    }

    public static final int MSG_NETWORK_ABORT = 2;
    public static final int MSG_NETWORK_ERROR = 3;
    public static final int MSG_RESPONSE_DATA_ERROR = 4;
    public static final int MSG_SUCCESS = 1;
    private Class<? extends T> clazz;
    private Handler mGuiHandler;
    private HttpConnection mHttpConnection;
    private Thread mHttpThread;

    /**
     * 初始化网络连接工具类和GUI Handler。
     */
    public AbstractHttpController(Class<? extends T> clazz) {
        this.clazz = clazz;
    }

    /**
     * 中断HTTP请求，并关闭HTTP连接。
     */
    public void abortConnection() {
        mHttpConnection.abort();
    }

    /**
     * 专用于Floor Person API的请求。特点是：参数名为“request“，参数值为一个字符串。
     */
    protected void sendRequest(UriConstants uri, String paramValue) {
        mHttpConnection = new HttpConnection();
        mGuiHandler = new HttpHandler<T>(this);
        mHttpThread = new Thread(new HttpThread(uri, paramValue));
        mHttpThread.start();
    }
}
